package com.adjy.pathbuilders.rendering;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

/**
 * GameCanvas is responsible for converting the coordinates from PhysicalWorld
 * into the Canvas keeping the ratio and maximum size of the World
 */
public class GameCanvas {

	private Canvas canvas;
	private Paint bitmapPaint;
	private Paint debugPaint;
	
	
	public GameCanvas() {
		this.bitmapPaint = new Paint();
		this.bitmapPaint.setAntiAlias(true);
		this.bitmapPaint.setFilterBitmap(true);
		this.bitmapPaint.setDither(true);
		
		debugPaint = new Paint();
		debugPaint.setColor(Color.RED);
	}
	
	/**
	 * Sets the Canvas to perform the Draws
	 * 
	 * @param canvas
	 *            Canvas to be used on draws
	 */
	public void setCanvas(Canvas canvas) {
		this.canvas = canvas;
	}

	/**
	 * Draw the text on Canvas (This method is the same as Canvas.drawText)
	 * 
	 * @param text
	 *            Text to be Draw
	 * @param x
	 *            X Position (Pixels) on Screen
	 * @param y
	 *            Y Position (Pixels) on Screen
	 * @param p
	 *            Paint to be used
	 */
	public void drawText(String text, int x, int y, Paint p) {
		if (isCanvasNull())
			return;
		canvas.drawText(text, x, y, p);
	}
	
	/**
	 * Draw the text on Canvas Converting from the Target Screen
	 * 
	 * @param text
	 *            Text to be Draw
	 * @param x
	 *            X Position (Pixels) on Target Screen
	 * @param y
	 *            Y Position (Pixels) on Target Screen
	 * @param p
	 *            Paint to be used
	 */
	public void drawTextWithConversion(String text, int x, int y, Paint p) {
		if (isCanvasNull())
			return;
		canvas.drawText(text, x * DisplayConverter.getScreenRatioWidth(),
				y * DisplayConverter.getScreenRatioHeight(), p);
	}
	
	/**
	 * Sets the current canvas to null
	 */
	public void nullfyCanvas() {
		this.canvas = null;
	}

	/**
	 * Gets the current Canvas
	 * 
	 * @return Canvas
	 */
	public Canvas getCanvas() {
		return canvas;
	}

	/**
	 * Fills the current canvas with the given color (This method is the same as
	 * Canvas.drawARGB)
	 * 
	 * @param a
	 *            Alpha Channel (0-255)
	 * @param r
	 *            Red Channel (0-255)
	 * @param g
	 *            Green Channel (0-255)
	 * @param b
	 *            Blue Channel (0-255)
	 */
	public void drawARGB(int a, int r, int g, int b) {
		if (isCanvasNull())
			return;
		canvas.drawARGB(a, r, g, b);
	}
	
	/**
	 * Fills the current canvas with the given color (This method is the same as
	 * Canvas.drawColor)
	 * 
	 * @param color
	 *            Color to fill the canvas
	 */
	public void drawColor(int color) {
		if (isCanvasNull())
			return;
		canvas.drawColor(color);
	}

	/**
	 * Fill the current Canvas with the given Drawable
	 * 
	 * @param bg
	 *            Bitmap to be used
	 */
	public void drawBackground(Bitmap bg) {
		if (isCanvasNull())
			return;
		canvas.drawBitmap(bg, null,
				new Rect(0, 0, canvas.getWidth(), canvas.getHeight())
				, bitmapPaint);
	}

	
	/*
	 * Checks if the Canvas is null
	 */
	private boolean isCanvasNull() {
		if (canvas == null)
			Log.e("Canvas", "No Canvas to Draw");
		return canvas == null;

	}
	
	public void drawBitmapFromPhysics(Bitmap bitmap, float x, float y) {
		if (isCanvasNull())
			return;
		
		Point p = DisplayConverter.convertFromPhysics(x, y);
		
		canvas.drawBitmap(bitmap,
				p.getX(), // X
				canvas.getHeight() - p.getY(), // Y
				bitmapPaint);
	}
	
	public void drawBitmap(Bitmap bitmap, float x, float y) {
		if (isCanvasNull())
			return;
		canvas.drawBitmap(bitmap, x, y, bitmapPaint);
	}

	public void drawDebugRect(float x, float y, float width, float height) {
		
		Point p = DisplayConverter.convertFromPhysics(x, y);
		Point p2 = DisplayConverter.convertFromPhysics(x + width, y - height);
		
		canvas.drawRect(p.getX(),
				canvas.getHeight() - p.getY(),
				p2.getX(),
				canvas.getHeight() - p2.getY(),
				debugPaint);
		
	}

	public void drawRect(float x, float y, float width, float height, Paint paint) {
		canvas.drawRect(x, y, x + width, y + height, paint);
	}
	
	public void drawRect(Rect r, Paint p) {
		canvas.drawRect(r, p);
	}
	
	public void drawRoundRect(float x, float y, float width, float height, Paint paint, float rx, float ry) {
		canvas.drawRoundRect(new RectF(x, y, x + width, y + height), rx, ry, paint);
	}
	
	public void drawTextConvertFromDisplay(String text, float x, float y, Paint paint) {
		Point p1 = DisplayConverter.convertFromPhysics(x, y);
		if (isCanvasNull())
			return;
		canvas.drawText(text, p1.getX(), p1.getY(), paint);
	}

	public void rotate(float f, float g, float h) {
		
		Point p = DisplayConverter.convertFromPhysics(g,h);
		canvas.rotate(f,  p.getX(), canvas.getHeight() - p.getY());
		
	}

	public void drawBitmapWithConversion(Bitmap bitmap, float x, float y) {
		drawBitmap(bitmap, x * DisplayConverter.getScreenRatioWidth(),
				y * DisplayConverter.getScreenRatioHeight());
	}
}
